package cn.itsource.org.service.impl;

import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    //注入mapper层对象
    @Autowired
    private EmployeeMapper mapper;

    @Override
    public List<Employee> findAll() {
        return mapper.selectAll();
    }

    @Override
    public Employee findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Employee employee) {
        if(employee.getId() == null){
            //只有设了值的字段会被新增进去
            mapper.insertSelective(employee);
        } else {
            //根据主键修改,只有设置了值的字段才会被新增进去
            mapper.updateByPrimaryKeySelective(employee);
        }
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageList<Employee> findPage(EmployeeQuery query) {
        //1.分页工具包
        PageHelper.startPage(query.getPage(), query.getRows());
        //2.调用方法
        List<Employee> employees = mapper.selectAll();
        //3.封装一个pageInfo对象
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        //4.封装自定义的分页对象
        PageList<Employee> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        return pageList;
    }

    @Override
    public List<Employee> findManagers() {
        List<Employee> managers = mapper.findManagers();
        return managers;
    }
}
